package oldtests;

import gui.BotGui;
import httpbot.DatabaseService;
import httpbot.HttpRequests;
import httpbot.IRequests;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.google.common.base.Preconditions;

import domain.Place;

import bot.City;
import bot.Util;

public class testMain {
//	public static void main(String[] args){
//		File in = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\Maine2Area.txt");
//		File out = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\Maine2Area2.txt");
//		
//		try{
//			BufferedReader reader = new BufferedReader(new FileReader(in));
//			BufferedWriter writer = new BufferedWriter(new FileWriter(out));
//			String line = null;
//			String output = "";
//			while ((line = reader.readLine()) != null){
//				String test[] = line.split(" ");
//				for (int i = 0; i < test.length; i++){
//					if (i == 1){
//						output += test[i].substring(0, test[i].length() - 3) + " ";
//					}
//					else{
//						output += test[i] + " ";
//					}
//				}
//				output += "\n";
//				writer.write(output);
//				output = "";
//			}
//			reader.close();
//			writer.close();
//		}
//		catch (FileNotFoundException e){
//			e.printStackTrace();
//		}
//		catch (IOException e){
//			e.printStackTrace();
//		}
//	}
	
	public static void main(String args[]){
		DatabaseService service = new DatabaseService();
		List<Place> places = service.getPlacesByState(DatabaseService.IOWA);
		for (Place place: places){
			System.out.println(place.getName() + " " + place.getType());
		}
		
//		IRequests requester = new HttpRequests();
//		String result = requester.getPageSource("Adair,_Iowa");
//		System.out.println(sanatize(result));
	}
	
	private static String sanatize(String input){
		Reader reader = null;
		String sanatized = null;
		try{
			//json structure query -> pages -> <pageid> -> revisions -> *
			reader = new StringReader(input);
			JSONParser parser = new JSONParser();
			JSONObject json = (JSONObject) parser.parse(reader);
			json = (JSONObject) json.get("query");
			json = (JSONObject) json.get("pages");
			Set<?> keys = json.keySet();
			Preconditions.checkArgument(keys.size() == 1);
			Iterator<?> iter = keys.iterator();
			json = (JSONObject) json.get(iter.next());
			JSONArray array = (JSONArray) json.get("revisions");
			json = (JSONObject) array.get(0);
			sanatized = (String) json.get("*");
		} 
		catch (IOException e){
			e.printStackTrace();
		} 
		catch (ParseException e) {
			e.printStackTrace();
		}
		finally{
			IOUtils.closeQuietly(reader);
		}
		return sanatized;
	}
}
